package com.akaovi.exp.utils;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Scanner;

/**
 * @author akaovi
 * @version 1.0
 * @description
 * @date 2022/11/14 14:12
 * @since 2022/11/14 14:12
 */
public class Utils {

    private static final Scanner scan = new Scanner(System.in);

    private static int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    /**
     * 判断年份是否合法
     *
     * @param year 年份
     * @return true or false
     */
    private static boolean isYear(int year) {
        return year > 1970 && year <= 9999;
    }

    /**
     * 判断月份是否合法
     *
     * @param month 月份
     * @return 合法与否
     */
    private static boolean isMonth(int month) {
        return month > 0 && month <= 12;
    }

    /**
     * 判断天数是否合法
     *
     * @param month 月份
     * @param day   日
     * @return 合法与否
     */
    private static boolean isDay(int month, int day) {
        return day > 0 && day <= days[month - 1];
    }

    public static LocalDate getCalender() {
        int year, month, day;
        while (true) {
            System.out.print("输入年月日: ");
            year = scan.nextInt();
            month = scan.nextInt();
            day = scan.nextInt();

            if (isYear(year) && isMonth(month) && isDay(month, day)) {
                break;
            }
            System.out.println("输入的是非法日期!");
        }
        return LocalDate.of(year, month, day);
    }

    public static void main(String[] args) {
        System.out.println(getCalender());
    }

}
